﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

/*
 * Author:Conis
 * Date:
 * Description:图片优化的操作助手
 */

namespace Conis.Squirrel
{
    /// <summary>
    /// 图片处理，可以转换图片为JPEG格式，图片裁剪，压缩图片，添加水印等
    /// </summary>
    public class PictureHelper
    {
        /// <summary>
        /// 缩放设置
        /// </summary>
        public struct ZoomConfig
        {
            public Size MaxSize;
            public bool LockScale;
            public int Quality;
        }


        public Bitmap Picture { get; set; }
        public ZoomConfig ZoomSetting { set; get; }

        public PictureHelper(string file)
        {
            this.Picture = new Bitmap(file);
        }

        public PictureHelper(Bitmap picture)
        {
            this.Picture = picture;
        }
        /// <summary>
        /// 转换图片
        /// </summary>
        /// <param name="maxSize"></param>
        /// <param name="lockScale"></param>
        public void Convert(string fileName)
        {
            Size size = this.ZoomSetting.MaxSize;
            ImageFormat thisFormat = this.Picture.RawFormat;
            if (this.ZoomSetting.LockScale)
            {
                size = this.GetSize(this.ZoomSetting.MaxSize, new Size(this.Picture.Width, this.Picture.Height));
            }

            Bitmap outBmp = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage(outBmp);
            Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);
            // 设置画布的描绘质量
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(outBmp, rect, 0, 0, size.Width, size.Height, GraphicsUnit.Pixel);
            g.Dispose();


            // 以下代码为保存图片时，设置压缩质量
            EncoderParameters encoderParams = new EncoderParameters();
            long[] picQuality = new long[1];
            picQuality[0] = this.ZoomSetting.Quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, picQuality);
            encoderParams.Param[0] = encoderParam;

            //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x];//设置JPEG编码
                    break;
                }
            }

            if (jpegICI != null)
            {
                outBmp.Save(fileName, jpegICI, encoderParams);
            }
            else
                outBmp.Save(fileName, thisFormat);

            outBmp.Dispose();
        }

        /// <summary>
        /// 添加文字水印
        /// </summary>
        /// <param name="text"></param>
        public void WaterMark(string text)
        {

        }

        /// <summary>
        /// 添加图片水印
        /// </summary>
        /// <param name="picture"></param>
        public void WaterMark(Bitmap picture)
        {

        }

        /// <summary>
        /// 保存成为一个文件
        /// </summary>
        /// <param name="fileName"></param>
        public void Save(string fileName)
        {

        }


        /// <summary>
        /// 获取图片大小，锁定长宽比
        /// </summary>
        /// <param name="maxSize">最大的Size</param>
        /// <param name="oldSize">旧的Size</param>
        /// <returns></returns>
        private Size GetSize(Size maxSize, Size oldSize)
        {
            //旧的长宽都比新的小
            if (oldSize.Width < maxSize.Width && oldSize.Height < maxSize.Height)
            {
                return oldSize;
            }


            if (NumericHelper.Div(oldSize.Width, oldSize.Height) > NumericHelper.Div(maxSize.Width, maxSize.Height))
            {
                double height = NumericHelper.Div(maxSize.Width * oldSize.Height, oldSize.Width);
                return new Size(maxSize.Width, (int)height);
            }

            double width = NumericHelper.Div(maxSize.Height * oldSize.Width, oldSize.Height);
            return new Size((int)width, maxSize.Height);
            /*
            double w = 0.0;
            double h = 0.0;
            double sw = Convert.ToDouble(width);
            double sh = Convert.ToDouble(height);
            double mw = Convert.ToDouble(maxWidth);
            double mh = Convert.ToDouble(maxHeight);

            if (sw < mw && sh < mh)
            {
                w = sw;
                h = sh;
            }
            else if ((sw / sh) > (mw / mh))
            {
                w = maxWidth;
                h = (w * sh) / sw;
            }
            else
            {
                h = maxHeight;
                w = (h * sw) / sh;
            }
            return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
             */

        }

        
    }
}
